package nc.ui.lmqth.iface;

import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;

public class StringUtil {
	public static boolean isEmpty(String str){
		return str == null || str.length() == 0;
	}
	
	public static String replaceAllString(String source, String strReplaced, String strReplace){
		if (isEmpty(source) || isEmpty(strReplaced) || strReplace == null)
			return source;
		StringBuffer buf = new StringBuffer(source.length());
		int start = 0;
		for (int end = 0; (end = source.indexOf(strReplaced, start)) != -1;)
		{
			buf.append(source.substring(start, end)).append(strReplace);
			start = end + strReplaced.length();
		}

		buf.append(source.substring(start));
		return buf.toString();
	}
	public static void createDirectoryAsNeeded(String strSource,String strTarget) throws IOException {
		if (strSource.indexOf("\\") >= 0)
			strSource = replaceAllString(strSource, "\\", "/");
		StringTokenizer st = new StringTokenizer(strSource, "/");
		if (strTarget == null && st.hasMoreElements())
			strTarget = st.nextElement().toString();
		StringBuffer newPath = new StringBuffer(strTarget);
		do {
			if (!st.hasMoreElements())
				break;
			String str = st.nextElement().toString();
			if (!st.hasMoreElements())
				break;
			newPath.append(File.separator);
			newPath.append(str);
			File dir = new File(newPath.toString());
			if (!dir.exists())
				dir.mkdir();
		} while (true);
	}
}
